<?php
namespace app\admin\controller;

use think\Request;

class Ad extends Common
{
    //轮播图首页
    public function index()
    {
        //升序排列
        $adlist = db('ad')->order('num asc')->paginate(5);
        $page=$adlist->render();
        //dump($page);die;
        $page = str_replace(
            ["pagination",
            "disabled",
            "active"],
        ["am-pagination tpl-pagination",
        "am-disabled",
        "am-active"],
        $page);
        // echo $page;die;
        if (Request::instance()->isAjax()) {
            return json(['adlist'=>$adlist , 'page'=>$page]);
        }else {
            $this->assign('adlist' , $adlist);
            $this->assign('page' , $page);
            return view();
        }
    }
    //轮播图添加页面
    public function add()
    {
        return view();
    }

    //上传轮播图
    public function upimg(){

        $file = request()->file('src');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $img_src = '/uploads/'.$info->getSaveName();
                //echo $img_src;
                //制作缩略图
                //打开图片
                $image = \think\Image::open('.'.$img_src);
                //echo $image;die();
                $date =date('Ymd',time());
                $path="uploads/thumbs/$date";
                //echo $path;die;
                if(!file_exists($path)){
                    mkdir($path, 0777,true);
                }
                //保存缩略图 前面要加点
                $image->thumb(150,100)->save('.'.'/uploads/thumbs/'.$info->getSaveName());
                $thumb_src='/uploads/thumbs/'.$info->getSaveName();
                //echo $thumb_src;die;
                return ['src'=>$img_src,'thumb_src'=>$thumb_src];
                // return ['src'=>$img_src];
            }
        }
        else{
                $this->error($file->getError());
            }
    }
    //新增添加入库
    public function add_ad(){
        $data = input('post.');
         //dump($data);die;
         $validate = validate('Ad');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
        $ret = db('ad')->insert($data);
        if($ret){
            $this->success('添加成功','admin/ad/index');
        }else{
            $this->error('添加失败');
        }
    }
    //轮播图编辑页面
    public function edit()
    {
        $id=input('param.id');
        $data = input('post.');
        $result = db('ad')->where("id=$id")->find();
        //dump($result);die;
        $this->assign('result',$result);
        return view();
    }

    //改轮播图
    public function update()
    {
        $id=input('param.id');
        $data=input('post.');
        $result = db('ad')->where("id=$id")->find();
        //dump($result);dump($data);die;
        $validate = validate('Ad');
        $rule =['num','link','miaosu'=>'require'];
        if(!empty($data['src'])){
            $rule[]='src';
            $rule[]='thumb_src';
        }
        //验证场景,验证想验证的
        $validate->scene('edit',$rule);
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error( $validate->getError());
        }
        if(empty($data['src'])){
            unset($data['src']);
        }
        if(empty($data['thumb_src'])){
            unset($data['thumb_src']);
        }
        $ret=db('ad')->where("id=$id")->update($data);
        if($ret == false){
            $this->error('修改失败');
        }else{
            $this->success('修改成功');
        }
    }

    //删除轮播图
    public function delete(){
        $id=$_GET['id'];
        $ret=db('ad')->where("id=$id")->delete();
        if($ret){
            $this->success("删除成功!");
        }else{
            $this->error("删除失败");
        }
    }
}
